﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 日铁.Modle;
using static NPOI.HSSF.UserModel.HeaderFooter;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace 日铁
{
    public partial class BudgetAdjustmentSheet : Form
    {

        Class1 one = new Class1();
        public BudgetAdjustmentSheet()
        {
            InitializeComponent();

            dateTimePicker1.Value = new DateTime(1900, 1, 1);
            
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BudgetAdjustmentsList annualBudgetList = new BudgetAdjustmentsList();
            annualBudgetList.Show();
            this.Close();
        }

        private void 审核ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                string BASID = row.Cells[0].Value.ToString();
                string Fyear = row.Cells[3].Value.ToString();
                try
                {
                    using (SqlConnection connection = DBHelper.CreateConnection())
                    {
                        string insertText = $"update T_Budget_adjustment_sheet set Fstatus='审核' where BASID='{BASID}'";
                        int rows = DBHelper.ExecuteNonQuery(insertText, connection, false, null);

                        
                        if (rows > 0)
                        {
                            DataTable dataTablew = DBHelper.ExecuteDataTable($"select * from T_Budget_adjustment_sheet_E where FID='{BASID}'", connection, false, null);
                            foreach (DataRow obj2 in dataTablew.Rows)
                            {
                                if (obj2.ItemArray[3].ToString() != null)
                                {
                                    string Fbtype = obj2.ItemArray[2].ToString();
                                    string Fmpurchase = obj2.ItemArray[3].ToString();
                                    string FNSCH = obj2.ItemArray[4].ToString();
                                    string FMID = obj2.ItemArray[5].ToString();
                                    string FMname = obj2.ItemArray[6].ToString();
                                    string FSAM = obj2.ItemArray[7].ToString();
                                    string Funit = obj2.ItemArray[8].ToString();
                                    string FRBA = obj2.ItemArray[9].ToString();
                                    string FRBQ = obj2.ItemArray[10].ToString();

                                    DataTable dataTable = DBHelper.ExecuteDataTable($"select * from T_Annual_budget_list_E where FMAterialID='{FMID}'", connection, false, null);
                                    int row3 = dataTable.Rows.Count;

                                    string FID = DBHelper.ExecuteScalar($"select ABIID from T_Annual_budget_list where Fyear='{Fyear}'", connection, false, null).ToString();

                                    if (row3 > 0)
                                    {
                                        string updateabl = $"update T_Annual_budget_list_E set FRBA={FRBA} , FRBQ={FRBQ} where FMAterialID='{FMID}' and FID={FID}";
                                        int ro = DBHelper.ExecuteNonQuery(updateabl, connection, false, null);
                                    }
                                    else
                                    {
                                        string insertABLE = $"insert into T_Annual_budget_list_E (FID, Fbtype, Fmpurchase,FNSCH,FMAterialID,FMname,FSAM,Funit,FIBA,FIBQ,FRBA,FRBQ) values('{FID}','{Fbtype}','{Fmpurchase}','{FNSCH}','{FMID}','{FMname}','{FSAM}','{Funit}','{FRBA}','{FRBQ}','{FRBA}','{FRBQ}')";
                                        int ro = DBHelper.ExecuteNonQuery(insertABLE, connection, false, null);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            MessageBox.Show("审核成功");
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                string ID = row.Cells[0].Value.ToString();
                try
                {

                    using (SqlConnection connection = DBHelper.CreateConnection())
                    {
                       // string picture = DBHelper.ExecuteScalar($"select BASID from T_Budget_adjustment_sheet  WHERE BASID='{ID}'", connection, false, null).ToString();

                        string DELETEable = $"DELETE T_Budget_adjustment_sheet_E WHERE FID='{ID}'";
                        int rows2 = DBHelper.ExecuteNonQuery(DELETEable, connection, false, null);

                        string DELETEabl = $"DELETE T_Budget_adjustment_sheet WHERE BASID='{ID}'";
                        int rows = DBHelper.ExecuteNonQuery(DELETEabl, connection, false, null);


                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            GetBAS();
        }


        private void BudgetAdjustmentSheet_Load(object sender, EventArgs e)
        {
            Listname();
            GetBAS();
            this.WindowState = FormWindowState.Maximized;
        }
        public void GetBAS()
        {
            try
            {
                DataTable BAS = one.BAS();
                using (SqlConnection connection = DBHelper.CreateConnection())
                {
                    
                    DataTable dataTable = DBHelper.ExecuteDataTable($"select * from T_Budget_adjustment_sheet as a left join T_Budget_adjustment_sheet_E as b on a.BASID=b.FID", connection, false, null);
                    
                    if (dataTable.Rows.Count > 0)
                    {
                        string index = "";
                        foreach (DataRow row in dataTable.Rows)
                        {
                            
                            if (index != (row["BASID"]).ToString())
                            {
                                BAS.Rows.Add((row["BASID"]).ToString(), (row["Fnumber"]).ToString(), (row["Fstatus"]).ToString(), (row["Fyear"]).ToString(), (row["Fctime"]).ToString(), (row["Ffounder"]).ToString(), (row["Fatime"]).ToString(), (row["Fauditor"]).ToString(), (row["ID"]).ToString(), (row["FID"]).ToString(), (row["Fq"]).ToString(), (row["Fbtype"]).ToString(), (row["Fmpurchase"]).ToString(), (row["FNSCH"]).ToString(), (row["FMAterialID"]).ToString(), (row["FMname"]).ToString(), (row["FSAM"]).ToString(), (row["Funit"]).ToString(), (row["FBAA"]).ToString(), (row["FNBA"]).ToString());
                            }
                            else
                            {
                                BAS.Rows.Add((row["BASID"]).ToString(), "", "", "", "", "", "", "", (row["ID"]).ToString(), (row["FID"]).ToString(), (row["Fq"]).ToString(), (row["Fbtype"]).ToString(), (row["Fmpurchase"]).ToString(), (row["FNSCH"]).ToString(), (row["FMAterialID"]).ToString(), (row["FMname"]).ToString(), (row["FSAM"]).ToString(), (row["Funit"]).ToString(), (row["FBAA"]).ToString(), (row["FNBA"]).ToString());
                            }
                            index = (row["BASID"]).ToString();

                        }
                        dataGridView1.DataSource = BAS;
                    }
                    else
                    {
                        dataGridView1.DataSource = null;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            
        }

        public void Listname()
        {


            DataGridViewTextBoxColumn BASID = new DataGridViewTextBoxColumn();
            BASID.HeaderText = "预算类型";
            BASID.DataPropertyName = "BASID";
            BASID.Visible = false;
            dataGridView1.Columns.Add(BASID);

            DataGridViewTextBoxColumn Fnumber = new DataGridViewTextBoxColumn();
            Fnumber.HeaderText = "单据编号";
            Fnumber.DataPropertyName = "Fnumber";
            dataGridView1.Columns.Add(Fnumber);

            DataGridViewTextBoxColumn Fstatus = new DataGridViewTextBoxColumn();
            Fstatus.HeaderText = "单据状态";
            Fstatus.DataPropertyName = "Fstatus";
            dataGridView1.Columns.Add(Fstatus);

            DataGridViewTextBoxColumn Fyear = new DataGridViewTextBoxColumn();
            Fyear.HeaderText = "预算年度";
            Fyear.DataPropertyName = "Fyear";
            dataGridView1.Columns.Add(Fyear);

            DataGridViewTextBoxColumn Fctime = new DataGridViewTextBoxColumn();
            Fctime.HeaderText = "创建时间";
            Fctime.DataPropertyName = "Fctime";
            dataGridView1.Columns.Add(Fctime);

            DataGridViewTextBoxColumn Ffounder = new DataGridViewTextBoxColumn();
            Ffounder.HeaderText = "创建人";
            Ffounder.DataPropertyName = "Ffounder";
            dataGridView1.Columns.Add(Ffounder);

            DataGridViewTextBoxColumn Fatime = new DataGridViewTextBoxColumn();
            Fatime.HeaderText = "审核时间";
            Fatime.DataPropertyName = "Fatime";
            dataGridView1.Columns.Add(Fatime);

            DataGridViewTextBoxColumn Fauditor = new DataGridViewTextBoxColumn();
            Fauditor.HeaderText = "审核人";
            Fauditor.DataPropertyName = "Fauditor";
            dataGridView1.Columns.Add(Fauditor);

            DataGridViewTextBoxColumn ID = new DataGridViewTextBoxColumn();
            ID.HeaderText = "ID";
            ID.DataPropertyName = "ID";
            ID.Visible = false;
            dataGridView1.Columns.Add(ID);

            DataGridViewTextBoxColumn FID = new DataGridViewTextBoxColumn();
            FID.HeaderText = "FID";
            FID.DataPropertyName = "FID";
            FID.Visible = false;
            dataGridView1.Columns.Add(FID);

            DataGridViewTextBoxColumn Fq = new DataGridViewTextBoxColumn();
            Fq.HeaderText = "Fq";
            Fq.DataPropertyName = "Fq";
            Fq.Visible = false;
            dataGridView1.Columns.Add(Fq);

            DataGridViewTextBoxColumn Fbtype = new DataGridViewTextBoxColumn();
            Fbtype.HeaderText = "预算类型";
            Fbtype.DataPropertyName = "Fbtype";
            dataGridView1.Columns.Add(Fbtype);

            DataGridViewTextBoxColumn Fmpurchase = new DataGridViewTextBoxColumn();
            Fmpurchase.HeaderText = "采购方式";
            Fmpurchase.DataPropertyName = "Fmpurchase";
            dataGridView1.Columns.Add(Fmpurchase);

            DataGridViewTextBoxColumn FNSCH = new DataGridViewTextBoxColumn();
            FNSCH.HeaderText = "NSCH采购类型";
            FNSCH.DataPropertyName = "FNSCH";
            dataGridView1.Columns.Add(FNSCH);

            DataGridViewTextBoxColumn FMAterialID = new DataGridViewTextBoxColumn();
            FMAterialID.HeaderText = "物料代码";
            FMAterialID.DataPropertyName = "FMAterialID";
            dataGridView1.Columns.Add(FMAterialID);

            DataGridViewTextBoxColumn FMname = new DataGridViewTextBoxColumn();
            FMname.HeaderText = "物料名称";
            FMname.DataPropertyName = "FMname";
            dataGridView1.Columns.Add(FMname);

            DataGridViewTextBoxColumn FSAM = new DataGridViewTextBoxColumn();
            FSAM.HeaderText = "规格型号";
            FSAM.DataPropertyName = "FSAM";
            dataGridView1.Columns.Add(FSAM);

            DataGridViewTextBoxColumn Funit = new DataGridViewTextBoxColumn();
            Funit.HeaderText = "单位";
            Funit.DataPropertyName = "Funit";
            dataGridView1.Columns.Add(Funit);

            

            DataGridViewTextBoxColumn FBAA = new DataGridViewTextBoxColumn();
            FBAA.HeaderText = "年度预算调整金额";
            FBAA.DataPropertyName = "FBAA";
            dataGridView1.Columns.Add(FBAA);

            DataGridViewTextBoxColumn FNBA = new DataGridViewTextBoxColumn();
            FNBA.HeaderText = "年度预算调整数量";
            FNBA.DataPropertyName = "FNBA";
            dataGridView1.Columns.Add(FNBA);


            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void 搜索ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string DH = textBox1.Text;
            string YSND = numericUpDown1.Text;
            string Time = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            using (SqlConnection connection = DBHelper.CreateConnection())
            {
                DataTable dataTable = DBHelper.ExecuteDataTable($"select * from T_Budget_adjustment_sheet as a left join T_Budget_adjustment_sheet_E as b on a.BASID=b.FID", connection, false, null);
                if (DH != "")
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        // 检查条件  
                        if ((row["Fnumber"]).ToString() != DH)
                        {
                            // 标记行为删除  
                            row.Delete();
                        }
                    }

                    dataTable.AcceptChanges();
                }
                if (YSND != "0")
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        // 检查条件  
                        if ((row["Fyear"]).ToString() != DH)
                        {
                            // 标记行为删除  
                            row.Delete();
                        }
                    }

                    dataTable.AcceptChanges();
                }
                if (Time != "1900-01-01")
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        // 检查条件  
                        if ((row["Fctime"]).ToString() != DH)
                        {
                            // 标记行为删除  
                            row.Delete();
                        }
                    }
                    dataTable.AcceptChanges();
                }
                DataTable BAS = one.BAS();
                if (dataTable.Rows.Count > 0)
                {
                    string index = "";
                    foreach (DataRow row in dataTable.Rows)
                    {

                        if (index != (row["BASID"]).ToString())
                        {
                            BAS.Rows.Add((row["BASID"]).ToString(), (row["Fnumber"]).ToString(), (row["Fstatus"]).ToString(), (row["Fyear"]).ToString(), (row["Fctime"]).ToString(), (row["Ffounder"]).ToString(), (row["Fatime"]).ToString(), (row["Fauditor"]).ToString(), (row["ID"]).ToString(), (row["FID"]).ToString(), (row["Fq"]).ToString(), (row["Fbtype"]).ToString(), (row["Fmpurchase"]).ToString(), (row["FNSCH"]).ToString(), (row["FMAterialID"]).ToString(), (row["FMname"]).ToString(), (row["FSAM"]).ToString(), (row["Funit"]).ToString(), (row["FBAA"]).ToString(), (row["FNBA"]).ToString());
                        }
                        else
                        {
                            BAS.Rows.Add((row["BASID"]).ToString(), "", "", "", "", "", "", "", (row["ID"]).ToString(), (row["FID"]).ToString(), (row["Fq"]).ToString(), (row["Fbtype"]).ToString(), (row["Fmpurchase"]).ToString(), (row["FNSCH"]).ToString(), (row["FMAterialID"]).ToString(), (row["FMname"]).ToString(), (row["FSAM"]).ToString(), (row["Funit"]).ToString(), (row["FBAA"]).ToString(), (row["FNBA"]).ToString());
                        }
                        index = (row["BASID"]).ToString();

                    }
                    dataGridView1.DataSource = BAS;
                }
                else
                {
                    dataGridView1.DataSource = null;
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            int a = e.RowIndex;
            int b = e.ColumnIndex;
            if (a >= 0 && b >= 0)
            {
                object value = dataGridView1.Rows[a].Cells[0].Value;
                int BASID = int.Parse(value.ToString());
                string picture = "";
                using (SqlConnection connection = DBHelper.CreateConnection())
                {
                    picture = DBHelper.ExecuteScalar($"select Fnumber  from T_Budget_adjustment_sheet where  BASID ='{BASID}'", connection, false, null).ToString();
                }
                // 将value转换为适当的类型（如果需要的话）  
                // 例如，如果你知道这一列是字符串类型，你可以这样做：  
                string stringValue = value as string; // 使用as操作符进行安全转换  
                BudgetAdjustmentsList annualBudgetList = new BudgetAdjustmentsList(picture);
                annualBudgetList.Show();
                this.Close();
            }
        }
    }

}
